/**
 * Created by lizhe on 2018/4/16.
 */
var datas = [];
var first = [];
var second = [];
var third = [];
//获取一二级栏目列表
$(document).ready(function(){
    queryList();
    queryRecommend();
    queryBanner();
    queryNews();
    var t1;
    var flag = getCookie("flag");
    if(flag == null){
        var pic1 = document.getElementById("pic1");
        var pic2 = document.getElementById("pic2");
        var pic3 = document.getElementById("pic3");
        var p1 = 0;
        t1 = setInterval(function(){
            if(p1!= 150){
                pic1.style.opacity = parseFloat(pic1.style.opacity) + 0.05;
                pic2.style.opacity = parseFloat(pic2.style.opacity) + 0.05;
                pic3.style.opacity = parseFloat(pic3.style.opacity) + 0.05;
                p1++;
            }else{
                clearInterval(t1);
                flag = true;
                pic1.style.display = "none";
                pic2.style.display = "none";
                pic3.style.display = "none";
                document.getElementById("indexPage").style.opacity = 1;
                document.getElementById("welcomePage").style.display = "none";
                setCookieNow("flag","true");
            }
        },100);
    }else {
        document.getElementById("welcomePage").style.display = "none";
        document.getElementById("indexPage").style.opacity = 1;
        setCookieNow("flag", "true");
    }

    setTimeout(function(){
        document.getElementById("todayVisitor").innerHTML = getCookie("count");
        document.getElementById("allVisitor").innerHTML = getCookie("totalCount");
    },50);

    //校园新闻——更多
    $("#newsMore").click(function(){
        location.href = "more.html";
    });

    $("#welcomePage").click(function(){
        clearInterval(t1);
        flag = true;
        pic1.style.display = "none";
        pic2.style.display = "none";
        pic3.style.display = "none";
        document.getElementById("indexPage").style.opacity = 1;
        document.getElementById("welcomePage").style.display = "none";
        setCookieNow("flag","true");
    });
});


function queryList(){
    $.ajax({
        url : urladdr + "/web/menu/list",
        method : "GET",
        data : {

        },
        success : function(data){
            if(data.code == 0){
                datas.splice(0,datas.length);
                first.splice(0,first.length);
                second.splice(0,second.length);
                third.splice(0,third.length);
                $("#menu").empty();
                $.each(data.data,function(i,date){
                    var d ={
                        id:date.id,name:date.title,parentId:date.parentId,height:date.height,type:date.type,icon:date.icon
                    };
                    datas.push(d);
                });
                for(var i =0;i<datas.length;i++){
                    if(datas[i].height==0){
                        var d = {
                            id:datas[i].id,name:datas[i].name,parentId:datas[i].parentId,height:datas[i].height,type:datas[i].type,icon:datas[i].icon
                        };
                        first.push(d);
                    }
                    if(datas[i].height==1){
                        d = {
                            id:datas[i].id,name:datas[i].name,parentId:datas[i].parentId,height:datas[i].height,type:datas[i].type,icon:datas[i].icon
                        };
                        second.push(d);
                    }
                    if(datas[i].height==2){
                        d = {
                            id:datas[i].id,name:datas[i].name,parentId:datas[i].parentId,height:datas[i].height,type:datas[i].type,icon:datas[i].icon
                        };
                        third.push(d);
                    }
                }

                document.getElementById("menu").innerHTML = document.getElementById("menu").innerHTML +
                    '<li>'+
                    '<a href="index.html" style="background-color:#3e8937;">'+
                    '<div class="columnMidLink" style="visibility: visible"></div>'+
                    '<i class="fa"><img src="images/9902.png"></i>'+
                    '<span style="margin-left: 30px">校园首页</span>'+
                    '<div class="columnPoint" style="visibility: visible"></div>'+
                    '</a>'+
                    '<div class="columnLink"></div>'+
                    '</li>';
                //自定拟部分栏目
                $.each(first,function(i,date){
                    var x=0;
                    var content = '<ul class="menu-academico-sub">';
                    $.each(second,function(j,dateJ){
                        if(dateJ.type==1){
                            var href2 = "article.html?menuId="+dateJ.id+"&type="+date.type+"&next=1";
                        }else if(dateJ.type == 2){
                            href2 = "album.html?menuId="+dateJ.id+"&type="+date.type+"&next=1";
                        }else if(dateJ.type == 3){
                            href2 = "teacher.html?menuId="+dateJ.id+"&type="+date.type+"&next=1";
                        }else if(dateJ.type == 4){
                            href2 = "student.html?menuId="+dateJ.id+"&type="+date.type+"&next=1";
                        }else if(dateJ.type == 5){
                            var id = queryArticleId(dateJ.id);
                            href2 = "text.html?menuId="+dateJ.id+"&id="+id+"&next=1";
                        }else if(dateJ.type == 6){
                            href2 = "teacher2.html?menuId="+dateJ.id+"&type="+date.type+"&next=1";
                        }
                        if(dateJ.parentId == date.id){
                            x=1;
                            content = content + '<li><b class="hidden">'+dateJ.id+'</b>' +
                                '<a href='+href2+' style="background: #1B4A2B;box-shadow: 2px 4px 4px 0 rgba(0,0,0,0.50);" onmousemove="aHover(this)" onmouseout="aMove(this)">'+dateJ.name+'</a>' +
                                '<div class="columnLink2"></div></li>'
                        }
                    });
                    if(x==1){
                        var href = "#";
                    }else{
                        if(date.type==1){
                            href = "article.html?menuId="+date.id+"&type="+date.type+"&next=0";
                        }else if(date.type == 2){
                            href = "album.html?menuId="+date.id+"&type="+date.type+"&next=0";
                        }else if(date.type == 3){
                            href = "teacher.html?menuId="+date.id+"&type="+date.type+"&next=0";
                        }else if(date.type == 4){
                            href = "student.html?menuId="+date.id+"&type="+date.type+"&next=0";
                        }else if(date.type == 5){
                            var id = queryArticleId(date.id);
                            href = "text.html?menuId="+date.id+"&id="+id+"&next=0";
                        }else if(date.type == 6){
                            href = "teacher2.html?menuId="+date.id+"&type="+date.type+"&next=0";
                        }
                    }
                    if(content == '<ul class="menu-academico-sub">'){
                        document.getElementById("menu").innerHTML = document.getElementById("menu").innerHTML +
                            '<li>'+
                            '<a href='+href+'>'+
                            '<div class="columnMidLink"></div>'+
                            '<i class="fa"><img src="'+date.icon+'"></i>'+
                            '<b class="hidden">'+date.id+'</b>'+
                            '<span style="margin-left: 30px">'+date.name+'</span>'+
                            '<div class="columnPoint"></div>'+
                            '</a>'+
                            '<div class="columnLink"></div>'+
                            '</li>'
                    }else{
                        document.getElementById("menu").innerHTML = document.getElementById("menu").innerHTML +
                            '<li>'+
                            '<a href='+href+'>'+
                            '<div class="columnMidLink"></div>'+
                            '<i class="fa"><img src="'+date.icon+'"></i>'+
                            '<b class="hidden">'+date.id+'</b>'+
                            '<span style="margin-left: 30px">'+date.name+'</span>'+
                            '<div class="columnPoint"></div>'+
                            '</a>'+
                            '<div class="columnLink"></div>'+
                            content+
                            '</ul>' +
                            '</li>'
                    }
                });
                //链接栏目
                document.getElementById("menu").innerHTML = document.getElementById("menu").innerHTML +
                    '<li>'+
                    '<a href="schoolLink.html">'+
                    '<div class="columnMidLink"></div>'+
                    '<i class="fa"><img src="images/9_06.png"></i>'+
                    '<span style="margin-left: 30px">学校链接</span>'+
                    '<div class="columnPoint"></div>'+
                    '</a>'+
                    '<div class="columnLink"></div>'+
                    '</li>'+
                    '<li>'+
                    '<a href="otherLink.html">'+
                    '<div class="columnMidLink"></div>'+
                    '<i class="fa"><img src="images/9908.png"></i>'+
                    '<span style="margin-left: 30px">其他链接</span>'+
                    '<div class="columnPoint"></div>'+
                    '</a>'+
                    '<div class="columnLink"></div>'+
                    '</li>'+
                    '<li>' +
                    '<div align="center" style="background: rgb(38, 93, 58);height: 278px">'+
                    '<img src="images/code.png" class="codeImg">'+
                    '<div class="codeDiv">扫一扫关注</div>'+
                    '</div>'+
                    '</li>'
            }else if(data.code == 401){
                location.href = "index.html"
            }else{
                modalinfo(data.msg);
            }
        }
    });
}

function queryRecommend(){
    $.ajax({
        url : urladdr + "/web/recommend/list",
        method : "POST",
        data : {

        },
        success : function(data){
            if(data.code == 0){
                $.each(data.data,function(i,date){
                    var title = "title"+i;
                    var type = "type"+i;
                    var menu = "menu"+i;
                    document.getElementById(title).innerHTML = date.title;
                    document.getElementById(type).innerHTML = date.type;
                    document.getElementById(menu).innerHTML = date.menuId;
                });
            }else if(data.code == 401){
                location.href = "index.html"
            }else{
                modalinfo(data.msg);
            }
        }
    });
}

function queryBanner(){
    $.ajax({
        url : urladdr + "/web/banner/list",
        method : "GET",
        data : {

        },
        success : function(data){
            if(data.code == 0){
                $("#sliderContent").empty();
                $.each(data.data,function(i,date){
                    var url = imgUrl + date.imageUrl;
                    document.getElementById("sliderContent").innerHTML = document.getElementById("sliderContent").innerHTML +
                        '<div class="item" style="background-position:initial;background-repeat: initial;cursor: pointer" onclick=openUrl("'+date.bannerUrl+'")>'+
                        '<a hidefocus="true" style="outline: none;">'+
                        '<img src="'+url+'" style="width: 860px;height: 100%"  alt="">'+
                        '</a>'+
                        '</div>'
                });
                $('.slider').mobilyslider({
                    content:'.sliderContent',
                    children:'div',
                    transition:'horizontal',
                    animationSpeed:50,
                    autoplay: true,
                    autoplaySpeed:bannertime,
                    pauseOnHover:true,
                    bullets:true,
                    arrows:true,
                    arrowsHide:true,
                    prev:'prev',
                    next:'next',
                    animationStart: function(){},
                    animationComplete: function(){}
                });
            }else if(data.code == 401){
                location.href = "index.html"
            }else{
                modalinfo(data.msg);
            }
        }
    });
}

function queryNews(){
    $.ajax({
        url : urladdr + "/website/search",
        method : "GET",
        data : {
            page:1,
            pageSize:10,
            keys:""
        },
        success : function(data){
            if(data.code == 0){
                $("#newsList").empty();
                $.each(data.data.list,function(i,date){
                    if(date.type!=1){
                        var str = date.addTime;
                        str = str.split(" ");
                        var time = str[0];
                    }else{
                        var ext = JSON.parse(date.ext);
                        time = ext.submitTime;
                    }

                    var myDate = new Date();
                    var myYear = myDate.getFullYear();
                    var myMonth = myDate.getMonth() + 1;
                    var myDay = myDate.getDate();

                    var tDate = new Date(time);
                    var tYear = tDate.getFullYear();
                    var tMonth = tDate.getMonth() + 1;
                    var tDay = tDate.getDate();

                    if(myYear == tYear && myMonth == tMonth && myDay == tDay){
                        document.getElementById("newsList").innerHTML = document.getElementById("newsList").innerHTML +
                            '<div class="newsDiv" onclick=newSInfo('+date.menuId+','+date.type+','+date.id+')>'+
                            '<div class="newsLi">'+
                            '<a class="newsLia" style="cursor: pointer;font-weight: 600;">· '+date.title+'</a>'+
                            '</div>'+
                            '<div class="newsTime" style="color: #F35030;;">[ '+time+' ]</div>'+
                            '</div>'
                    }else{
                        document.getElementById("newsList").innerHTML = document.getElementById("newsList").innerHTML +
                            '<div class="newsDiv" onclick=newSInfo('+date.menuId+','+date.type+','+date.id+')>'+
                            '<div class="newsLi">'+
                            '<a class="newsLia" style="cursor: pointer;">· '+date.title+'</a>'+
                            '</div>'+
                            '<div class="newsTime">[ '+time+' ]</div>'+
                            '</div>'
                    }

                });
            }else if(data.code == 401){
                location.href = "index.html"
            }else{
                modalinfo(data.msg);
            }
        }
    });
}

function newSInfo(menuId,types,id){
    var next = "0";
    for (var i = 0; i < datas.length; i++) {
        if (datas[i].id == menuId) {
            break;
        }
    }
    if (datas[i].height != 0) {
        for (var z = 0; z < datas.length; z++) {
            if (datas[i].parentId == datas[z].id) {
                next = "1";
                break;
            }
        }
    }

    if(types == 1){
        location.href = "text.html?menuId="+menuId+"&id="+id+"&next="+next;
    }else if(types==2){
        location.href = "text.html?menuId="+menuId+"&id="+id+"&next="+next;
    }else if(types==3){
        location.href = "teacherText.html?menuId="+menuId+"&id="+id+"&next="+next;
    }else if(types == 4){
        location.href = "student.html?menuId="+menuId+"&type="+types+"&next="+next;
    }
}

function recommendJump(i){
    var href = "#";
    var typeId = "type"+i;
    var menu = "menu"+i;
    var menuId = document.getElementById(menu).innerHTML;
    var type = document.getElementById(typeId).innerHTML;
    for(var j =0 ;j<datas.length;j++){
        if(datas[j].id==menuId){
            if(datas[j].height == 1){
                var x = 0;
                for(var k = 0 ; k < third.length;k++){
                    if(third[k].parentId == datas[j].id){
                        x = 1;
                        break;
                    }
                }
                if(x==1){   //x=1表示存在下级目录
                    if(datas[j].type == 1){
                        href = "article.html?menuId="+menuId+"&type="+datas[j].type+"&next=1";
                    }else if(datas[j].type == 2){
                        href = "album.html?menuId="+menuId+"&type="+datas[j].type+"&next=1";
                    }else if(datas[j].type == 3){
                        href = "teacher.html?menuId="+menuId+"&type="+datas[j].type+"&next=1";
                    }else if(datas[j].type == 4){
                        href = "student.html?menuId="+menuId+"&type="+datas[j].type+"&next=1";
                    }
                }else{
                    if(datas[j].type == 1){
                        href = "article.html?menuId="+menuId+"&type="+datas[j].type+"&next=1";
                    }else if(datas[j].type == 2){
                        href = "album.html?menuId="+menuId+"&type="+datas[j].type+"&next=1";
                    }else if(datas[j].type == 3){
                        href = "teacher.html?menuId="+menuId+"&type="+datas[j].type+"&next=1";
                    }else if(datas[j].type == 4){
                        href = "student.html?menuId="+menuId+"&type="+datas[j].type+"&next=1";
                    }
                }
            }else if(datas[j].height == 2){
                x = 0;
                for(var k = 0 ; k < third.length;k++){
                    if(third[k].parentId == datas[j].id){
                        x = 1;
                        break;
                    }
                }
                if(x==1){   //x=1表示存在下级目录
                    if(datas[j].type == 1){
                        href = "article.html?menuId="+menuId+"&type="+datas[j].type+"&next=1";
                    }else if(datas[j].type == 2){
                        href = "album.html?menuId="+menuId+"&type="+datas[j].type+"&next=1";
                    }else if(datas[j].type == 3){
                        href = "teacher.html?menuId="+menuId+"&type="+datas[j].type+"&next=1";
                    }else if(datas[j].type == 4){
                        href = "student.html?menuId="+menuId+"&type="+datas[j].type+"&next=1";
                    }
                }else{
                    if(datas[j].type == 1){
                        href = "article.html?menuId="+menuId+"&type="+datas[j].type+"&next=1";
                    }else if(datas[j].type == 2){
                        href = "album.html?menuId="+menuId+"&type="+datas[j].type+"&next=1";
                    }else if(datas[j].type == 3){
                        href = "teacher.html?menuId="+menuId+"&type="+datas[j].type+"&next=1";
                    }else if(datas[j].type == 4){
                        href = "student.html?menuId="+menuId+"&type="+datas[j].type+"&next=1";
                    }
                }
            }
            break;
        }

    }
    location.href = href;
}